package com.woparker.kiosk.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Base64;
import android.util.Log;


import com.woparker.kiosk.bean.CachBean;
import com.woparker.kiosk.config.Constant;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.util.Map;

/**
 * Created by yuanxd on 2017/10/10 0010.
 */

public class SharedPreferencesUtil {
    /**
     * 保存在手机里面的名字
     */
    public static final String FILE_NAME = "kiosk_shared_data";
    private  SharedPreferences.Editor editor;
    private static SharedPreferencesUtil sharedPreferencesUtil;


    private static ByteArrayInputStream bais;

    private  SharedPreferences sharedPreferences;

    public static final String LANGUAGE = "LANGUAGE";


    private SharedPreferencesUtil(Context context){
        sharedPreferences = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
    }

    public static SharedPreferencesUtil getInstance(Context context){
        if(null  == sharedPreferencesUtil){
            sharedPreferencesUtil = new SharedPreferencesUtil(context);
        }
        return sharedPreferencesUtil;
    }


    /**
     * 保存数据的方法，拿到数据保存数据的基本类型，然后根据类型调用不同的保存方法
     *
     * @param key
     * @param object
     */
    public  void put(String key, Object object) {

        if (object instanceof String) {
            editor.putString(key, (String) object);
        } else if (object instanceof Integer) {
            editor.putInt(key, (Integer) object);
        } else if (object instanceof Boolean) {
            editor.putBoolean(key, (Boolean) object);
        } else if (object instanceof Float) {
            editor.putFloat(key, (Float) object);
        } else if (object instanceof Long) {
            editor.putLong(key, (Long) object);
        } else {
            editor.putString(key, object.toString());
        }
        editor.commit();
    }

    /**
     * 获取保存数据的方法，我们根据默认值的到保存的数据的具体类型，然后调用相对于的方法获取值
     *
     * @param key           键的值
     * @param defaultObject 默认值
     * @return
     */

    public  Object get(String key, Object defaultObject) {
        if (defaultObject instanceof String) {
            return sharedPreferences.getString(key, (String) defaultObject);
        } else if (defaultObject instanceof Integer) {
            return sharedPreferences.getInt(key, (Integer) defaultObject);
        } else if (defaultObject instanceof Boolean) {
            return sharedPreferences.getBoolean(key, (Boolean) defaultObject);
        } else if (defaultObject instanceof Float) {
            return sharedPreferences.getFloat(key, (Float) defaultObject);
        } else if (defaultObject instanceof Long) {
            return sharedPreferences.getLong(key, (Long) defaultObject);
        } else {
            return sharedPreferences.getString(key, null);
        }

    }







    /**
     *
     * @Title: savaObj
     * @Description: TODO(这里用一句话描述这个方法的作用)
     * @param @param key
     * @param @param obj    设定文件
     * @return void    返回类型
     * @throws
     */
    public synchronized void savaObj(String key,Object obj){
        try {
            ByteArrayOutputStream baos=new ByteArrayOutputStream();
            ObjectOutputStream oos=new ObjectOutputStream(baos);
            oos.writeObject(obj);
            String newWord = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));
            editor.putString(key, newWord).commit();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     *
     * @Title: getObj
     * @Description: TODO(这里用一句话描述这个方法的作用)
     * @param @param key
     * @param @param obj
     * @param @return    设定文件
     * @return T    返回类型
     * @throws
     */
    public synchronized  Object getObj(String key){
        String wordBase64 = sharedPreferences.getString(key, "");
        if(wordBase64.equals("")) {
            Log.e("staring", "为空");
            return null;
        }
        byte[] base64Bytes = Base64.decode(wordBase64 .getBytes(),Base64.DEFAULT);

        if(bais!=null){
            bais.reset();
        }
        bais = new ByteArrayInputStream(base64Bytes);
        ObjectInputStream ois;
        Object object = null;
        try {
            ois = new ObjectInputStream(bais);
            object=(Object) ois.readObject();
        } catch (StreamCorruptedException e) {
            e.printStackTrace();
        }  catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return object;
    }


    /**
     * 获取缓存对象
     * @return
     */
    public CachBean getCachBean(){
       CachBean cachBean =  (CachBean)getObj(Constant.SharedPConstant.SP_CASH_BEAN);
        if(null == cachBean){
            //当缓存数据为空则填充默认数据
            cachBean = new CachBean();
            cachBean.setQueryType(Constant.QueryConstant.QUERY_TYPE_DIM);
            cachBean.setDefaultCity(Constant.DEFOULT_CITY);
            cachBean.setAutoTime(Constant.SystemConstant.AUTO_BACK_TIME+"秒");
            cachBean.setSizeType(Constant.SIZE_ONE);
            cachBean.setIp(Constant.URLConstant.BASE_IP);
            cachBean.setPointX(Constant.POINT_X);
            cachBean.setPointY(Constant.POINT_X);
            cachBean.setLocation(Constant.LOCATION);
            cachBean.setToken(Constant.ACCOUNT);
            savaObj(Constant.SharedPConstant.SP_CASH_BEAN,cachBean);
        }
        return cachBean;
    }

    public  int getInt(String key, int defValue) {
        return sharedPreferences.getInt(key, defValue);
    }

    public SharedPreferences.Editor getEditor() {
        return editor;
    }

    /**
     * 缓存保存
     * @param cachBean
     */
    public void saveCachBean(CachBean cachBean){
        savaObj(Constant.SharedPConstant.SP_CASH_BEAN,cachBean);
    }

    /**
     * 移除某个key值已经对应的值
     *
     * @param key
     */
    public  void remove(String key) {
        editor.remove(key);
        editor.commit();
    }

    /**
     * 清除所有的数据
     */
    public  void clear() {
        editor.clear();
        editor.commit();
    }

    public void setLanguage(String value){
        put(LANGUAGE,value);
    }

     public String getLanguage(String defalutValue){
         return (String) get(LANGUAGE,defalutValue);
     }

    /**
     * 查询某个key是否存在
     *
     * @param key
     * @return
     */
    public  boolean contains(String key) {
        return sharedPreferences.contains(key);
    }

    /**
     * 返回所有的键值对
     *
     * @return
     */
    public  Map<String, ?> getAll() {
        return sharedPreferences.getAll();
    }



}
